package com.atguigu.dao.impl;

import com.atguigu.dao.ServiceDao;
import com.atguigu.pojo.Service;
import com.atguigu.utils.WebUtils;

import java.util.List;


public class ServiceDaoImpl extends BaseDao implements ServiceDao {

    @Override
    public int addService(Service service) {
        System.out.println("ServiceDaoImpl程序在[" + Thread.currentThread().getName() + "]线程中");
        String sql = "insert into t_service(`id`,`serverId`,`name`,`version`,`ports`,`customer`,`programmer`,`endTime`) values(?,?,?,?,?,?,?,?);";
        return update(sql,service.getId(), service.getServerId(),service.getName(), service.getVersion(),service.getPorts(), service.getCustomer(), service.getProgrammer(),service.getEndTime());
    }

    @Override
    public int deleteServiceById(String id) {
        String sql = "delete from t_service where id=?";
        return update(sql, id);
    }

    @Override
    public int updateService(Service service) {
        String sql = "update t_service set `serverId`=?,`name`=?,`version`=?,`ports`=?,`customer`=?,`programmer`=?,`endTime`=? where id=?";
        return update(sql, service.getServerId(),service.getName(), service.getVersion(), service.getPorts(), service.getCustomer(), service.getProgrammer(),service.getEndTime(), service.getId());
    }

    @Override
    public Service queryServiceById(String id) {
        String sql = "select `id`,`serverId`,`name`,`version`,`ports`,`customer`,`programmer`,`endTime` from t_service where id=?";
        return queryForOne(Service.class, sql, id);
    }
    @Override
    public List<Service> queryServicesByName(String serviceName) {
        String sql = "select `id`,`serverId`,`name`,`version`,`ports`,`customer`,`programmer`,`endTime` from t_service where name like ?";
        return queryForList(Service.class, sql, serviceName);
    }

    @Override
    public List<Service> queryServicesByServerId(String serverId) {// 查询全部
        String sql = "select `id`,`serverId`,`name`,`version`,`ports`,`customer`,`programmer`,`endTime` from t_service where serverId=?";
        return queryForList(Service.class, sql,serverId);
    }
    @Override
    public List<Service> queryServicesByServerIdAndServiceName(String serverId,String serviceName){
        String sql = "select `id`,`serverId`,`name`,`version`,`ports`,`customer`,`programmer`,`endTime` from t_service where serverId=? and name like ?";
        return queryForList(Service.class, sql,serverId,serviceName);
    }
    @Override
    public List<Service> queryServices() {// 查询全部
        String sql = "select `id`,`serverId`,`name`,`version`,`ports`,`customer`,`programmer`,`endTime` from t_service";
        return queryForList(Service.class, sql);
    }

    @Override
    public Integer queryForPageTotalCount() {//查询总条目数,为page分片做准备
        String sql = "select count(*) from t_service";
        Number serviceCount = (Number) queryForSingleValue(sql);//这个语句似乎可以返回任何类型的查询
        //System.out.println(bookCount);
        return serviceCount.intValue();
    }

    @Override
    public List<Service> queryForPageItems(Integer begin, Integer pageSize) {
        String sql = "select `id`,`serverId`,`name`,`version`,`ports`,`customer`,`programmer`,`endTime` from t_service limit ?,?";
        return queryForList(Service.class, sql, begin, pageSize);//查询数据片段
    }
}


